package com.luke.web.controller.base;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import com.luke.common.config.LuKeConfig;
import com.luke.common.constant.Constants;
import com.luke.common.utils.file.FileUploadUtils;
import com.luke.common.utils.file.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.luke.common.annotation.Log;
import com.luke.common.core.controller.BaseController;
import com.luke.common.core.domain.AjaxResult;
import com.luke.common.enums.BusinessType;
import com.luke.base.domain.LkVehicle;
import com.luke.base.service.ILkVehicleService;
import com.luke.common.utils.poi.ExcelUtil;
import com.luke.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车型管理Controller
 * 
 * @author luke
 * @date 2021-05-30
 */
@RestController
@RequestMapping("/base/vehicle")
public class LkVehicleController extends BaseController
{
    @Autowired
    private ILkVehicleService lkVehicleService;

    /**
     * 查询车型管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:vehicle:list')")
    @GetMapping("/list")
    public TableDataInfo list(LkVehicle lkVehicle)
    {
        startPage();
        List<LkVehicle> list = lkVehicleService.selectLkVehicleList(lkVehicle);
        return getDataTable(list);
    }

    /**
     * 查询所有车型
     */
    @GetMapping("/vehicleModelList")
    public AjaxResult vehicleModelList()
    {
        List<String> list = lkVehicleService.selectVehicleModelList();
        return AjaxResult.success(list);
    }
    /**
     * 根据车型查询车辆颜色
     */
    @GetMapping("/vehicleColorList/{vehicleMode}")
    public AjaxResult vehicleColorList(@PathVariable("vehicleMode") String vehicleMode)
    {
        List<String> list = lkVehicleService.selectVehicleColorList(vehicleMode);
        return AjaxResult.success(list);
    }

    /**
     * 导出车型管理列表
     */
    @PreAuthorize("@ss.hasPermi('base:vehicle:export')")
    @Log(title = "车型管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LkVehicle lkVehicle)
    {
        List<LkVehicle> list = lkVehicleService.selectLkVehicleList(lkVehicle);
        ExcelUtil<LkVehicle> util = new ExcelUtil<LkVehicle>(LkVehicle.class);
        return util.exportExcel(list, "车型管理数据");
    }


    /**
     * 车型图片上传
     */
    @PostMapping("/uploadVehicleImage")
    public AjaxResult uploadVehicleImage(@RequestParam("files") MultipartFile[] files) throws IOException
    {
        AjaxResult ajax=new AjaxResult();
        String filepath="";
        for (MultipartFile file:files){
            String path = FileUploadUtils.upload(LuKeConfig.getVehicleImagePath(), file);
            if(StringUtils.isBlank(filepath)){
                filepath+=path;
            }else {
                filepath=filepath+","+path;
            }
        }
        ajax.put("imgUrl", filepath);
        return ajax;
    }

    /**
     * 获取车型管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:vehicle:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(lkVehicleService.selectLkVehicleById(id));
    }

    /**
     * 新增车型管理
     */
    @PreAuthorize("@ss.hasPermi('base:vehicle:add')")
    @Log(title = "车型管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LkVehicle lkVehicle)
    {
        lkVehicle.setOnlineTime(new Date());
        return toAjax(lkVehicleService.insertLkVehicle(lkVehicle));
    }

    /**
     * 修改车型管理
     */
    @PreAuthorize("@ss.hasPermi('base:vehicle:edit')")
    @Log(title = "车型管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LkVehicle lkVehicle)
    {
        String[] vehicleDelImage=lkVehicle.getVehicleDelImage();
        for (String s : vehicleDelImage) {
            String localPath = LuKeConfig.getProfile();
            String filePath = localPath + StringUtils.substringAfter(s, Constants.RESOURCE_PREFIX);
            FileUtils.deleteFile(filePath);
        }
        return toAjax(lkVehicleService.updateLkVehicle(lkVehicle));
    }

    /**
     * 删除车型管理
     */
    @PreAuthorize("@ss.hasPermi('base:vehicle:remove')")
    @Log(title = "车型管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(lkVehicleService.deleteLkVehicleByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('base:vehicle:edit')")
    @Log(title = "车型管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody LkVehicle lkVehicle)
    {
        if("0".equals(lkVehicle.getSaleStatus())){
            lkVehicle=lkVehicleService.selectLkVehicleById(lkVehicle.getId());
            lkVehicle.setSaleStatus("0");
            if(lkVehicle.getSaleTime()==null){
                lkVehicle.setSaleTime(new Date());
            }
        }
        return toAjax(lkVehicleService.updateLkVehicle(lkVehicle));
    }
}
